package com.monitor.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.monitor.dao.ErpMaterielMapper;
import com.monitor.entity.ErpContractCommodity;
import com.monitor.entity.ErpMateriel;
import com.monitor.service.ErpContractCommodityService;
import com.monitor.service.ErpContractCustomerService;
import com.monitor.service.ErpCustomerService;
import com.monitor.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 合同协议-物料价格表 前端控制器
 * </p>
 *
 * @author LX
 * @since 2018-09-17
 */
@RestController
@RequestMapping("/erpContractCommodity")
@Api(description="合同协议-商品价格")
public class ErpContractCommodityController {

    @Resource
    private ErpContractCommodityService commodityService;
    
    @Resource
    private ErpMaterielMapper erpMaterielMapper;

    @Resource
    private ErpContractCustomerService contractCustomerService;
    @Resource
    private ErpCustomerService customerService;





    @RequestMapping("/inertOrUpdateCommodity")
    @ApiOperation("添加Or修改物料价格信息")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "custId", value = "客户ID", required = false, dataType = "int", paramType = "query")*/
    })
    public @ResponseBody
    Result<Integer> inertOrUpdateCommodity(ErpContractCommodity contract) {
        Result<Integer> result=new Result<Integer>();
        try{
        	if(contract.getId()==null||contract.getId()==0) {
        		int materielId=1;
        		List<ErpMateriel> list=erpMaterielMapper.selectList(new EntityWrapper<ErpMateriel>().eq("materiel_name", contract.getComName()).and().eq("materiel_Specifications", contract.getComSpec())
        				.and().eq("materiel_brand", contract.getComBrand()).and().eq("materiel_model", contract.getComMdel()).and().eq("materiel_place", contract.getComAddress()));
        		if(list.size()>0) {
        			materielId=list.get(0).getId();
        		}
        		contract.setMaterielId(materielId);
        	}
        	
            result.setCount(1);
            result.setData(commodityService.insertOrUpdate(contract)?1:0);
        }catch (Exception e){e.printStackTrace();}
        return result;
    }

    @RequestMapping("/getCommoditytById")
    @ApiOperation("根据物料价格id物料价格信息")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),*/
            @ApiImplicitParam(name = "contractId", value = "物料价格ID", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody
    Result<List<ErpContractCommodity>> getCommoditytById(Integer contractId) {
        Result<List<ErpContractCommodity>> result=new Result();
        try{
            result.setCount(1);
            result.setData(commodityService.selectList(new EntityWrapper<ErpContractCommodity>().eq("id",contractId)));
        }catch (Exception e){e.printStackTrace();}
        return result;
    }


    @RequestMapping("/getCommoditytByConId")
    @ApiOperation("根据协议id物料价格信息")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),*/
            @ApiImplicitParam(name = "contractId", value = "物料价格ID", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody
    Result<List<ErpContractCommodity>> getCommoditytByConId(Integer contractId) {
        Result<List<ErpContractCommodity>>  result=new Result<>();
        try{
            result.setCount(1);
            result.setData(commodityService.selectList(new EntityWrapper<ErpContractCommodity>().eq("contract_id",contractId)));
        }catch (Exception e){e.printStackTrace();}
        return result;
    }

    @RequestMapping("/getCommodityById")
    @ApiOperation("根据物料价格id物料价格信息")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),*/
            @ApiImplicitParam(name = "contractId", value = "物料价格ID", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody
    Result<List<ErpContractCommodity>> getCommodityById(Integer contractId) {
        Result<List<ErpContractCommodity>> result=new Result<List<ErpContractCommodity>>();
        try{
            result.setCount(1);
            List<ErpContractCommodity> erpContractCommodities = commodityService.selectList(new EntityWrapper<ErpContractCommodity>().eq("pid", contractId));

            result.setData(erpContractCommodities);
        }catch (Exception e){e.printStackTrace();}
        return result;
    }

    @RequestMapping("/removetCommodityById")
    @ApiOperation("根据物料价格id删除物料价格信息")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),*/
            @ApiImplicitParam(name = "contractId", value = "物料价格ID", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody
    Result<Integer> removetCommodityById(Integer contractId) {
        Result<Integer> result=new Result<Integer>();
        try{
            result.setCount(1);
            result.setData(commodityService.deleteById(contractId)?1:0);
        }catch (Exception e){e.printStackTrace();}
        return result;
    }

  @RequestMapping("/getContractBycustIdPage")
    @ApiOperation("根据客户ID查询物料价格信息分页")
    @ApiImplicitParams({
           //* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "custId", value = "客户ID", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody
    Result<Page<ErpContractCommodity>> getContractBycustIdPage(@RequestParam(value = "page", defaultValue = "1") int page,
                                                               @RequestParam(value="limit",defaultValue="10") int limit, Integer custId) {
        Result<Page<ErpContractCommodity>> result=new Result<Page<ErpContractCommodity>>();
        try{
            Page<ErpContractCommodity> page1=new Page<ErpContractCommodity>(page,limit);
            Page<ErpContractCommodity> carPage=commodityService.selectPage(page1,new EntityWrapper<ErpContractCommodity>().eq("customer_id",custId));
            result.setCount(carPage.getRecords().size());
            result.setData(carPage);
        }catch (Exception e){e.printStackTrace();}
        return result;
    }

}

